package com.example.a2209_shop.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.a2209_shop.R
import com.example.a2209_shop.adapter.GoodsAdapter
import com.example.a2209_shop.adapter.OneAdapter
import com.example.a2209_shop.adapter.TwoAdapter
import com.example.a2209_shop.databinding.FragmentCateBinding
import com.example.a2209_shop.fgViewModel.CateViewModel
import com.example.base.mvvm.BaseFragment

class CateFragment : BaseFragment<FragmentCateBinding,CateViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsAdapter: GoodsAdapter

    var cateId = 14
    var page = 1
    override fun initData() {
        //一级分类请求成功
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //二级列表初始化化
                val data = it.data[0].data
                if(data!=null){
                    twoAdapter.data.clear()
                    twoAdapter.data.addAll(data)
                    twoAdapter.notifyDataSetChanged()
                    //三级列表初始化
                    cateId = twoAdapter.data[0].id
                    viewModel.goods(cateId,page,10)
                }
            }
        }
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        //初始化三个列表
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,5)

        goodsAdapter = GoodsAdapter()
        binding.rvThree.adapter = goodsAdapter
        binding.rvThree.layoutManager = LinearLayoutManager(context)

        //请求一级分类
        viewModel.cate()

        //点击一级的分类  二级三级数据可联动
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setId(oneAdapter.data[position].id)//添加背景色
            //二级三级数据先清空
            twoAdapter.data.clear()
            twoAdapter.notifyDataSetChanged()
            goodsAdapter.data.clear()
            goodsAdapter.notifyDataSetChanged()

            val data = oneAdapter.data[position].data
            if(data!=null&&data.isNotEmpty()){//二级数据不为null并且存在数据才进行初始化
                twoAdapter.setId(data[0].id)
                twoAdapter.data.addAll(data)
                twoAdapter.notifyDataSetChanged()

                cateId = twoAdapter.data[0].id
                viewModel.goods(cateId,1,10)
            }
        }
        //二级分类点击
        twoAdapter.setOnItemClickListener { adapter, view, position ->

            twoAdapter.setId(twoAdapter.data[position].id)
            //三级数据清空
            goodsAdapter.data.clear()
            goodsAdapter.notifyDataSetChanged()

            cateId = twoAdapter.data[position].id
            viewModel.goods(cateId,1,10)

        }
        //三级分类点击
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }

    }

    override fun getViewModel(): Class<CateViewModel>  = CateViewModel::class.java

    override fun getLayout(): Int = R.layout.fragment_cate

}